package com.eagle.gennewdata;
import java.io.IOException;
import java.util.Date;
import java.util.zip.CRC32;
import javax.servlet.http.*;


import com.eagle.gennewdata.logic.INewData;
import com.eagle.gennewdata.logic.NewDataImpl;
import com.eagle.gennewdata.model.WebSetting;
import com.eagle.gennewdata.untl.GlobalsFun;

@SuppressWarnings("serial")
public class GennewdataServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		int nPage = 1;
		boolean bRefrash = true;
		if (req.getParameter("refrash") != null)
			bRefrash = true;
		
		if (req.getParameter("page") != null) 
			nPage = Integer.valueOf(req.getParameter("page").toString());
		
		ListNewDataServlet.s_bIsRunGen = true;
		Date dateNow = new Date();
		long timeLastGetTime  = dateNow.getTime() - WebSetting.timeLastGetTime;
		if (timeLastGetTime > WebSetting.timeGetNewGapTime || bRefrash) {
			long nCrc = GlobalsFun.getChecksumValue(new CRC32(), GlobalsFun.getUrlHtmlText(WebSetting.strGetNewUrlSource));
			if (nCrc != WebSetting.crcCheckSum || bRefrash) {
				INewData inewdata = new NewDataImpl();
				for (int i = 1; i <= nPage; i++) 
					inewdata.PaserXmlUrl(WebSetting.strGetNewUrl + String.format("?page=%d", i));
				WebSetting.timeLastGetTime = dateNow.getTime();
				WebSetting.crcCheckSum = nCrc;	
			}
		}
		ListNewDataServlet.s_bIsRunGen = false;
		resp.setContentType("text/plain;charset=utf-8");
		resp.getWriter().println(dateNow.toString());
	}
	
	
	
}
